home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n02 / sfedit.exe / SFEDIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-27  |  8.7 KB  |  303 lines

  1. #define NOCOMM
  2. #include <windows.h>
  3. #include <io.h>
  4. #include <stdarg.h>
  5. #include <direct.h>
  6. #include "sfedit.h"
  7. #include "sfiledlg.h"
  8.  
  9. #define ID_EDITWND (100)
  10. #define MAX_EDITSIZE (32767)
  11.  
  12. /* Function Prototypes */
  13. LONG FAR PASCAL _export WndProc(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam);
  14. int  SFMsgBox(WORD wType, LPSTR lpszFmt, ...);
  15. BOOL GetFile(HWND hWndEdit, LPSTR szFileSpec);
  16. BOOL PutFile(HWND hWndEdit, LPSTR szFileSpec);
  17.  
  18. /* Global Variables */
  19. extern HANDLE _hInstance;
  20. HWND _hWndMain;
  21. char _szAppName[] = "SFEdit";
  22.  
  23. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
  24.     int nCmdShow)
  25. {
  26.     HANDLE      hAccel;
  27.     MSG         msg;
  28.  
  29.     if (!hPrevInstance)
  30.     {
  31.         WNDCLASS    wndclass;
  32.  
  33.         wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  34.         wndclass.lpfnWndProc    = WndProc;
  35.         wndclass.cbClsExtra     = 0;
  36.         wndclass.cbWndExtra     = 0;
  37.         wndclass.hInstance      = hInstance;
  38.         wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  39.         wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  40.         wndclass.hbrBackground  = GetStockObject(WHITE_BRUSH);
  41.         wndclass.lpszMenuName   = "MENU_SFEDIT";
  42.         wndclass.lpszClassName  = _szAppName;
  43.  
  44.         RegisterClass(&wndclass);
  45.     }
  46.  
  47.     _hInstance = hInstance;
  48.  
  49.     _hWndMain = CreateWindow(
  50.         _szAppName, _szAppName,
  51.         WS_OVERLAPPEDWINDOW,
  52.         CW_USEDEFAULT, CW_USEDEFAULT,
  53.         CW_USEDEFAULT, CW_USEDEFAULT,
  54.         NULL, NULL, hInstance, NULL
  55.         );
  56.  
  57.     ShowWindow(_hWndMain, nCmdShow);
  58.     UpdateWindow(_hWndMain);
  59.  
  60.     hAccel = LoadAccelerators(hInstance, _szAppName);
  61.  
  62.     while(GetMessage(&msg, NULL, 0, 0))
  63.     {
  64.         if (!TranslateAccelerator(_hWndMain, hAccel, &msg))
  65.         {
  66.             TranslateMessage(&msg);
  67.             DispatchMessage(&msg);
  68.         }
  69.     }
  70.     return  msg.wParam;
  71. }
  72.  
  73. LONG FAR PASCAL _export WndProc(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  74. {
  75.     HMENU           hMenu;
  76.  
  77.     static char     szFileSpec[128];
  78.     static char     szTmp[128];
  79.     static char     szOpenFileName[128];
  80.     static HWND     hWndEdit;
  81.     static SFDlg    sfdlg;
  82.     static SFType   typeList[] = {
  83.         {"*.TXT", "Text files (*.TXT)"     },
  84.         {"*.C",   "C source (*.C)"         },
  85.         {"*.H",   "Header files (*.H)"     },
  86.         {"*.RC",  "Resource scripts (*.RC)"},
  87.         {"*.*",   "All files (*.*)"        },
  88.         {"", ""}
  89.     };
  90.  
  91.     switch(wMsg)
  92.     {
  93.       case WM_CREATE :
  94.         hWndEdit = CreateWindow("EDIT", NULL,
  95.             WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE |
  96.             WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  97.             0,0,0,0, hWnd, ID_EDITWND, _hInstance, NULL);
  98.  
  99.         SendMessage(hWndEdit, EM_LIMITTEXT, MAX_EDITSIZE, 0L);
  100.  
  101.         hMenu = GetMenu(hWnd);
  102.         EnableMenuItem(hMenu, IDM_SAVE,   MF_GRAYED);
  103.         EnableMenuItem(hMenu, IDM_SAVEAS, MF_GRAYED);
  104.  
  105.         getcwd(szFileSpec, 128);
  106.         return  0L;
  107.  
  108.       case WM_DESTROY :
  109.         PostQuitMessage(0);
  110.         return  0L;
  111.  
  112.       case WM_SIZE :
  113.         MoveWindow(hWndEdit, 0,0, LOWORD(lParam), HIWORD(lParam), TRUE);
  114.         return  0L;
  115.  
  116.       case WM_SETFOCUS :
  117.         SetFocus(hWndEdit);
  118.         return  0L;
  119.  
  120.       case WM_COMMAND :
  121.         switch (wParam)
  122.         {
  123.           case IDM_OPEN :
  124.             sfdlg.hWnd         = hWnd;
  125.             sfdlg.lpTypes      = typeList;
  126.             sfdlg.nType        = 0;
  127.             sfdlg.lpszFileSpec = szFileSpec;
  128.             sfdlg.nMaxFileSpec = sizeof(szFileSpec);
  129.             sfdlg.lpszTitle    = "File Open";
  130.  
  131.             lstrcpy(szFileSpec, typeList[sfdlg.nType].szFile);
  132.  
  133.             if (SFileDlg(&sfdlg) == IDOK)
  134.             {
  135.                 if (GetFile(hWndEdit, szFileSpec))
  136.                 {
  137.                     lstrcpy(szOpenFileName, szFileSpec);
  138.  
  139.                     wsprintf(szTmp, "%s - %s", (LPSTR) _szAppName,
  140.                         (LPSTR) szFileSpec);
  141.                     SetWindowText(hWnd, szTmp);
  142.  
  143.                     hMenu = GetMenu(hWnd);
  144.                     EnableMenuItem(hMenu, IDM_SAVE,   MF_ENABLED);
  145.                     EnableMenuItem(hMenu, IDM_SAVEAS, MF_ENABLED);
  146.                 }
  147.             }
  148.  
  149.             return  0L;
  150.  
  151.           case IDM_SAVE :
  152.             PutFile(hWndEdit, szOpenFileName);
  153.             return  0L;
  154.  
  155.           case IDM_SAVEAS :
  156.             lstrcpy(szFileSpec, szOpenFileName);
  157.  
  158.             sfdlg.hWnd         = hWnd;
  159.             sfdlg.lpTypes      = typeList;
  160.             sfdlg.nType        = 0;
  161.             sfdlg.lpszFileSpec = szFileSpec;
  162.             sfdlg.nMaxFileSpec = sizeof(szFileSpec);
  163.             sfdlg.lpszTitle    = "File Save As";
  164.  
  165.             if (SFileDlg(&sfdlg)==IDOK)
  166.             {
  167.                 if (PutFile(hWndEdit, szFileSpec))
  168.                 {
  169.                     lstrcpy(szOpenFileName, szFileSpec);
  170.  
  171.                     wsprintf(szTmp, "%s - %s", (LPSTR) _szAppName, (LPSTR) szFileSpec);
  172.                     SetWindowText(hWnd, szTmp);
  173.                 }
  174.             }
  175.             return  0L;
  176.  
  177.           case IDM_ABOUT:
  178.             wsprintf(szTmp, "%s\xA9 1992, Paul Chui.", (LPSTR)_szAppName);
  179.             SFMsgBox(MB_OK|MB_ICONINFORMATION, szTmp);
  180.             return  0L;
  181.  
  182.           case IDM_EXIT :
  183.             DestroyWindow(hWnd);
  184.             return  0L;
  185.         }
  186.       return  0L;
  187.     }
  188.  
  189.     return  DefWindowProc(hWnd, wMsg, wParam, lParam);
  190. }
  191.  
  192. /****************************************************************************
  193.     BOOL GetFile(HWND hWndEdit, LPSTR lpszFileSpec)
  194.  
  195.     PURPOSE: Opens a file and then fills hWndEdit with the file.
  196.  
  197.     PARAMETERS:
  198.         HWND    hWndEdit            Edit control to recieve text file
  199.         LPSTR   lpszFileSpec        Default name in file combo box
  200.  
  201.     RETURNS: TRUE if a new file is opened.
  202. *****************************************************************************/
  203. BOOL GetFile(HWND hWndEdit, LPSTR lpszFileSpec)
  204. {
  205.     DWORD       dwFileSize;
  206.     int         hFile;
  207.     HANDLE      hFileBuf;
  208.     LPSTR       lpFileBuf;
  209.     OFSTRUCT    of;
  210.  
  211.     if ((hFile = OpenFile(lpszFileSpec, &of, OF_READ)) == -1)
  212.     {
  213.         SFMsgBox(MB_OK|MB_ICONEXCLAMATION, "DOS error %d opening file '%s'.",
  214.             of.nErrCode, (LPSTR) lpszFileSpec);
  215.         return  FALSE;
  216.     }
  217.  
  218.     if ((dwFileSize = filelength(hFile)) >= MAX_EDITSIZE)
  219.     {
  220.         SFMsgBox(MB_OK|MB_ICONEXCLAMATION,"File '%s' is too big.",
  221.             (LPSTR) lpszFileSpec);
  222.         _lclose(hFile);
  223.         return  FALSE;
  224.     }
  225.  
  226.     hFileBuf  = GlobalAlloc(GHND, dwFileSize + 1);
  227.     lpFileBuf = GlobalLock(hFileBuf);
  228.  
  229.     _lread(hFile, lpFileBuf, (WORD) dwFileSize);
  230.     *(lpFileBuf + (WORD)dwFileSize) = '\0';
  231.     SetWindowText(hWndEdit, lpFileBuf);
  232.  
  233.     GlobalUnlock(hFileBuf);
  234.     GlobalFree(hFileBuf);
  235.     _lclose(hFile);
  236.  
  237.     return  TRUE;
  238. }
  239.  
  240. /****************************************************************************
  241.     BOOL PutFile(HWND hWndEdit, LPSTR lpszFileSpec)
  242.  
  243.     PURPOSE: Saves the file contained in the text control.
  244.  
  245.     PARAMETERS:
  246.         HWND    hWndEdit            Edit control to containing the text file
  247.         LPSTR   lpszFileSpec        Default name of the file to be saves
  248.  
  249.     RETURNS: TRUE if a new file is saved.
  250. *****************************************************************************/
  251. BOOL PutFile(HWND hWndEdit, LPSTR lpszFileSpec)
  252. {
  253.     WORD        wEditLen;
  254.     int         hFile;
  255.     HANDLE      hFileBuf;
  256.     NPSTR       npFileBuf;
  257.     OFSTRUCT    of;
  258.  
  259.     if (OpenFile(lpszFileSpec, &of, OF_READ|OF_EXIST) > 0)
  260.     {
  261.         if (SFMsgBox(MB_YESNO|MB_ICONQUESTION, "Overwrite file '%s'?",
  262.             (LPSTR) lpszFileSpec) != IDYES)
  263.             return  FALSE;
  264.     }
  265.  
  266.     if ((hFile = OpenFile(lpszFileSpec, &of, OF_CREATE|OF_REOPEN)) == -1)
  267.     {
  268.         SFMsgBox(MB_OK|MB_ICONEXCLAMATION, "Error creating file '%s'.",
  269.             (LPSTR) lpszFileSpec);
  270.         return  FALSE;
  271.     }
  272.  
  273.     hFileBuf = (HANDLE) SendMessage(hWndEdit, EM_GETHANDLE, 0, 0L);
  274.     npFileBuf = LocalLock(hFileBuf);
  275.  
  276.     wEditLen = GetWindowTextLength(hWndEdit);
  277.     if (_lwrite(hFile, npFileBuf, wEditLen) != wEditLen)
  278.     {
  279.         SFMsgBox(MB_OK|MB_ICONEXCLAMATION,  "Error writing file '%s'.",
  280.             (LPSTR) lpszFileSpec);
  281.         _lclose(hFile);
  282.         return  FALSE;
  283.     }
  284.  
  285.     LocalUnlock(hFileBuf);
  286.     _lclose(hFile);
  287.  
  288.     return  TRUE;
  289. }
  290.  
  291. int SFMsgBox(WORD wType, LPSTR lpszFmt, ...)
  292. {
  293.     static char     szBuf[80];
  294.     va_list         ap;
  295.  
  296.     va_start(ap, lpszFmt);
  297.     wvsprintf(szBuf, lpszFmt, ap);
  298.     va_end(ap);
  299.  
  300.     return MessageBox(_hWndMain, szBuf, _szAppName, wType);
  301. }
  302.  
  303.